use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                    port => 4444, 
                                    browser => "*firefox", 
                                    browser_url => "http://localhost.com" );

$sel->open_ok("/");
#$sel->click_ok("log_in_top");
$sel->wait_for_page_to_load_ok("30000");
$sel->is_element_present_ok("link=Log out");
$sel->click_ok("link=Administration");
$sel->wait_for_page_to_load_ok("30000");
$sel->click_ok("link=Bug Prediction");
$sel->text_is("link=Train Bug Prediction", "Train Bug Prediction");
$sel->click_ok("link=Train Bug Prediction");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_element_present("train_severity_product") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->text_is("//td[\@id='content']/h3[1]", "Train Bug Prediction for a single component");
$sel->text_is("//td[\@id='content']/p[1]", "Select a product and component for which to train the bug severity classifier:");
$sel->text_is("//td[\@id='content']/h3[2]", "Train Bug Prediction for all components");
$sel->text_is("//td[\@id='content']/p[2]", "This button will update the Bug Prediction for all products and components.");
$sel->value_is("train_classifier", "Train Classifier");
$sel->value_is("train-everything", "Train Everything");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_text_present("SeleniumTest") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->select_ok("train_severity_product", "label=SeleniumTest");
$sel->mouse_down_ok("//td[\@id='content']/p[1]");
WAIT: {
    for (1..60) {
        if (eval { "SeleniumTest" eq $sel->get_text("train_severity_component") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->select_ok("train_severity_component", "label=SeleniumTest");
$sel->click_ok("train_classifier");
WAIT: {
    for (1..60) {
        if (eval { "Done training component!" eq $sel->get_text("//td[\@id='content']/p") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->text_is("//td[\@id='content']/p", "Done training component!");
